package org.com.Controller;


import Entity.Shop;
import jakarta.servlet.http.HttpServletRequest;
import org.com.Service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 277
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @PostMapping("/uploadimg")
    public String upload(MultipartFile file, HttpServletRequest request) {
        return shopService.upload(file, request);
    }

    @PostMapping("/save")
    public Boolean save(@RequestBody Shop shop) {

        shop.setImgurl(shop.getImgurl());
        return shopService.save(shop);
    }

    @GetMapping("/list")
    public List<Shop> list() {
        return shopService.list();
    }

    @GetMapping("/image/{filename}")
    public ResponseEntity<byte[]> getImage(@PathVariable String filename) throws IOException {
        // 这里可以调用你的getimg方法
        byte[] imageBytes = shopService.getimg(filename);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 根据实际图片类型调整
        headers.setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS));

        return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
    }
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return shopService.removeById(id);
    }
}
